﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TSPquestEDITOR
{
    public partial class basic_info : Form
    {
        private void perf_input_chk()
        {

            SqlConnection qCon = new SqlConnection(connectionString);

            try
            {
                qCon.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            var input_cred = input_chk();

            if (!string.IsNullOrEmpty(input_cred))
            {
                if (input_cred.ToString() == "NO_NAME_ENTERED")
                {
                    MessageBox.Show("Error Tag: " + input_cred.ToString() + "\nDescription: No Name has been specified", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (input_cred.ToString() == "TOO_MANY_INPUTS_SPECIFIED")
                {
                    MessageBox.Show("Error Tag: " + input_cred.ToString() + "\nDescription: I can only search by NAME or ID not both.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (input_cred.ToString() == "SEARCH_RES_CANCELLED_BY_USER")
                {
                    MessageBox.Show("Error Tag: " + input_cred.ToString() + "\nDescription: You have cancelled Search By Name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (input_cred.ToString() == "SUCCESS")
                {
                    MessageBox.Show("SUCCESS");
                }
            }
        }

        public string input_chk()
        {
            try
            {

                //Define Connection
                SqlConnection qCon = new SqlConnection(connectionString);

                try
                {
                    //Open Connection
                    qCon.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                

                //We need to declare the name/id inputs
                string search_input_name_var = search_input_name.Text;
                string search_input_id_var = "";

                //Now we need to set them to the user input
                search_input_name_var = search_input_name.Text;

                if (!string.IsNullOrEmpty(multi_quest_search_return_id))
                {
                    search_input_name_var = "";
                    search_input_id_var = multi_quest_search_return_id;
                }

                //First let's make sure the variable is even being used
                //If both of the Variables are Blank
                if (string.IsNullOrEmpty(search_input_name_var) && string.IsNullOrEmpty(search_input_id_var))
                {
                    return "NO_NAME_ENTERED";
                }

                //If Just the Name Input is available
                if (!string.IsNullOrEmpty(search_input_name_var) && string.IsNullOrEmpty(search_input_id_var))
                {

                    //We need to see if there is anything matching the name input
                    SqlCommand search_name = qCon.CreateCommand();
                    search_name.CommandText = "SELECT count(*) FROM dbo.StringResource WHERE value LIKE '%" + search_input_name_var + "%'";
                    int search_name_res = ((int)search_name.ExecuteScalar());

                    //Now we check the count (if too high, return that we need to open the full results form)
                    if (search_name_res > 1)
                    {

                        multi_quest_search_name = search_input_name_var;

                        MessageBox.Show("Please select the appropriate Quest", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        search_input_name.Text = "";
                        Form2 form2 = new Form2();
                        form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
                        form2.ShowDialog();

                        return "SEARCH_COMPLETED";
                    }

                }
                if (string.IsNullOrEmpty(search_input_name_var) && !string.IsNullOrEmpty(search_input_id_var))
                {
                    fin_qID = Convert.ToInt32(search_input_id_var);
                    load_primary_quest_info();
                }
            }
            catch
            {
                return "INPUT_CHECK_FAILED!";
            }
            return "";
        }
    }
}
